package com.xiaochong.tir.apiservice.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.netflix.discovery.converters.Auto;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.apiservice.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.apiservice.config.ExecutorConfig;
import com.xiaochong.tir.apiservice.util.StringUtil;
import com.xiaochong.tir.common.data.hbasepo.DiagnosisScore;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.mapper.ExchangeTransactionpairMiddleMapper;
import com.xiaochong.tir.common.data.mapper.KLineMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.po.KLine;
import com.xiaochong.tir.common.data.redisPo.KActionTotalPo;
import com.xiaochong.tir.common.data.vo.ApiCurrencyDiagnosisScoreVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationResultVo;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import com.xiaochong.tir.common.util.OssImageUtil;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.MathContext;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.tir.apiservice.scheduled
 * @Description : TODO 行情是数据刷新接口
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月01日下午2:35
 * @ModificationHistory Who When What
 */
@Component
public class CurrencyQuotationScheduled {

    private static Semaphore semaphore = new Semaphore(1);

    @Autowired
    private CurrencyInfoMapper currencyInfoMapper;

    @Autowired
    private RedisServer redisServer;

    @Autowired
    private KLineMapper kLineMapper;

    @Autowired
    private OSSTool ossTool;

    @Autowired
    private OssImageUtil ossImageUtil;

    @Autowired
    private ExchangeTransactionpairMiddleMapper exchangeTransactionpairMiddleMapper;

    @Autowired
    private HbaseBaseQueryComponent hbaseBaseQueryComponent;

    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(fixedRate = 1000*10)
    public void currencyQuotation(){

        if(semaphore.tryAcquire(1)){
            try {
                LogTrace.beginTrace();
                List<CurrencyQuotationResultVo> quotationList=new ArrayList<>(  );

                CurrencyInfo currencyInfo = new CurrencyInfo();
                currencyInfo.setEnableFlag( true );
                currencyInfo.setDeleteFlag( false );
                currencyInfo.setCurrencyDiagnosisFlag( true );
                List<CurrencyInfo> currencyList = currencyInfoMapper.selectByEntityList( currencyInfo );

                for (int i = 0; i < currencyList.size(); i++) {
                    CurrencyInfo info = currencyList.get( i );
                    long beginTime = System.currentTimeMillis();
                    if(!StringUtils.isEmpty(info.getCmcId())){
                        CurrencyQuotationResultVo currencyQuotationResultVo = getCurrencyByCmcId( info.getCmcId() );
                        if(currencyQuotationResultVo != null ){
                            quotationList.add( currencyQuotationResultVo );
                        }
                    }else {
                        LogTrace.info("currencyQuotationScheduled","getCurrencyByCmcIdIsNull" , info.toString());
                    }
                    LogTrace.info("currencyQuotationScheduled","getCurrencyByCmcId" , info.getCmcId()+"耗时："+(System.currentTimeMillis()-beginTime));
                }
                redisServer.set( RedisKeysUtils.CURRENCY_MARKET_All,JSON.toJSONString( quotationList ));
            }catch (Exception e){
                e.printStackTrace();
                LogTrace.error("货币行情" ,e);
            }finally {
                semaphore.release(1);
                LogTrace.endTrace();
            }
        }
    }


    public CurrencyQuotationResultVo getCurrencyByCmcId(String cmcId) {
        if(StringUtils.isEmpty(cmcId)){ return null;}

        /**
         * 实时的交易所数据
         */
        String redisCacheKey =  RedisKeysUtils.CURRENCY_MARKET+cmcId;

        /**
         * 5分钟的CMC数据
         */
        String redisCacheKeyCmc =  RedisKeysUtils.CURRENCY_MARKET+"@"+cmcId;

        String result=redisServer.get( redisCacheKeyCmc);
        if (StringUtils.isNotBlank(result) && redisServer.exists(redisCacheKeyCmc)){
            LogTrace.info("getCurrencyByCmcId@cmc" , "resultKey" , redisCacheKeyCmc );
            CurrencyQuotationResultVo currencyQuotationResultVo = JSON.parseObject( result,CurrencyQuotationResultVo.class );
            //最新价格
            ExchangeTransactionpairMiddle exchangeTransactionpairMiddle=new ExchangeTransactionpairMiddle();
            exchangeTransactionpairMiddle.setTransactionName(cmcId);
            List<ExchangeTransactionpairMiddle> exchangeTransactionpairMiddleList = exchangeTransactionpairMiddleMapper.getTransactionPairByCurrencyInfo( exchangeTransactionpairMiddle );
            if(exchangeTransactionpairMiddleList.size()>0){
                exchangeTransactionpairMiddle = exchangeTransactionpairMiddleList.get( 0 );
                String Rediskey = RedisKeysUtils.API_SERVICE_K_ACTION_ALL_DATA_PRICE + "@" + exchangeTransactionpairMiddle.getExchangeId()+ "@" + exchangeTransactionpairMiddle.getTransactionName();
                String API_SERVICE_K_ACTION_CHANGE_PRICE = redisServer.get( Rediskey );
                if(API_SERVICE_K_ACTION_CHANGE_PRICE!=null){
                    KActionTotalPo rActionTotalPo = JSON.parseObject( API_SERVICE_K_ACTION_CHANGE_PRICE,KActionTotalPo.class );
                    currencyQuotationResultVo.setPriceCny( rActionTotalPo.getOpenPriceCny()!=null? getConverCut(rActionTotalPo.getOpenPriceCny()).doubleValue():0);
                    currencyQuotationResultVo.setChangFlag(rActionTotalPo.getChangFlag()!=null?rActionTotalPo.getChangFlag():0);
                    //今日涨幅
                    currencyQuotationResultVo.setTodayGains( rActionTotalPo.getChangePrice()!=null?Double.parseDouble( rActionTotalPo.getChangePrice() ):0.00);

                    //差值计算
                    double difference=currencyQuotationResultVo.getPriceCny()*currencyQuotationResultVo.getTodayGains();
                    currencyQuotationResultVo.setDifference(getDoubleTwo( difference/100 ));
                }
            }
            redisServer.set( redisCacheKey,JSON.toJSONString( currencyQuotationResultVo ));
            return currencyQuotationResultVo;
        }

        try {
            CurrencyQuotationResultVo currencyQuotationResultVo=new CurrencyQuotationResultVo();
            CurrencyQuotationResultVo currencyQuotationResultVo2=new CurrencyQuotationResultVo();
            CurrencyInfo info = getCurrencyInfo( cmcId );
            currencyQuotationResultVo.setName( info.getSymbol()+"-"+info.getCoinNameCn() );
            currencyQuotationResultVo.setCmcId( info.getCmcId() );
            currencyQuotationResultVo.setSymbol(info.getSymbol());
            currencyQuotationResultVo.setCoinNameCn( info.getCoinNameCn() );
            currencyQuotationResultVo.setCoinNameEn( info.getCoinNameEn() );
            if(info.getImageUrl()!=null){
                if(info.getImageUrl().contains( "http" )){
                    currencyQuotationResultVo.setImageUrl( info.getImageUrl() );
                }else{
                    String imgUrl = ossTool.getOSSUrl( info.getImageUrl() );
                    currencyQuotationResultVo.setImageUrl( imgUrl );
                }
            }
            currencyQuotationResultVo.setId( info.getId().toString() );

            int d=0;
            String grailStr =null;

            String rowKey = cmcId + "@" +DateUtils.getNextDayDateYMD(-d);
            LogTrace.info("getCurrencyByCmcId" , "hbaseBaseQueryComponent-begin" , rowKey);

            grailStr = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_NAME
                    , HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_FAMILY
                    , HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_QUALIFIER
                    , rowKey
            );
            if (grailStr!=null){
                JSONObject json = JSON.parseObject( grailStr );
                JSONArray adviceFirst = null;
                JSONArray advice = null;
                if (json != null) {
                    Set<String> setString=json.keySet();
                    int firstKey=24;
                    int lastKey=0;
                    for (String str : setString) {
                        int minStr=Integer.valueOf( str );
                        if (minStr<firstKey){
                            firstKey=minStr;
                        }
                    }
                    for (String str : setString) {
                        int maxStr=Integer.valueOf( str );
                        if (maxStr>lastKey){
                            lastKey=maxStr;
                        }
                    }
                    adviceFirst = (JSONArray) json.getJSONArray( firstKey+"" );
                    advice = (JSONArray) json.getJSONArray( lastKey+"" );
                }

                if (adviceFirst!=null){
                    JSONObject obj=adviceFirst.getJSONObject( 0 );
                    if (obj.containsKey("priceCny")) currencyQuotationResultVo2.setPriceCny( getConverCut(  obj.getString("priceCny")  ).doubleValue() ); //货币价格
                }

                if (advice!=null){
                    JSONObject obj=advice.getJSONObject( advice.size()-1 );
                    if (obj.containsKey("marketCapCny"))currencyQuotationResultVo.setMarketCapCny(RetainCount( Double.parseDouble(obj.get("marketCapCny")+""),2 ));  //流通市值
                    if (obj.containsKey("circulation24hCny")) currencyQuotationResultVo.setCirculatingSupply(RetainCount( Double.parseDouble( obj.get("circulation24hCny").toString()),0 ));   //流通数量
                    if (obj.containsKey("volume24hCny")) currencyQuotationResultVo.setVolume24h(RetainCount( Double.parseDouble(obj.get("volume24hCny")+""),0 ));//成交额
                    if (obj.containsKey("percentChange24h")) currencyQuotationResultVo.setChange24h(RetainCount( Double.parseDouble(obj.get("percentChange24h")+""),2 )); //24小时涨幅
                    if (obj.containsKey("percentChange1h")) currencyQuotationResultVo.setChange1h(RetainCount( Double.parseDouble(obj.get("percentChange1h")+""),2 )); //1小时涨幅
                    if (obj.containsKey("rank")) currencyQuotationResultVo.setMarkRank(obj.get("rank").toString()); //流通市值排名字段
                    if (obj.containsKey("priceCny")) currencyQuotationResultVo.setPriceCny( getConverCut(  obj.getString("priceCny")  ).doubleValue() ); //货币价格Cny
                    if (currencyQuotationResultVo2.getPriceCny()!=null&&currencyQuotationResultVo2.getPriceCny()>0) currencyQuotationResultVo.setTodayGains( StringUtil.getDoubleTwo( ( currencyQuotationResultVo.getPriceCny()-currencyQuotationResultVo2.getPriceCny() )/currencyQuotationResultVo2.getPriceCny()*100 ) ); //今日涨幅
                    currencyQuotationResultVo.setDifference( StringUtil.getDoubleTwo( currencyQuotationResultVo.getPriceCny()*currencyQuotationResultVo.getTodayGains()/100 ) ); //差值
                    if (obj.containsKey("priceUsd")) currencyQuotationResultVo.setLastOpenPrice( String.valueOf( getConverCut(  obj.getString("priceUsd") ) ) ); //货币价格Usd
                    String CMC_PRICE_CHANGFLAG = redisServer.get(RedisKeysUtils.CMC_PRICE_CHANGFLAG+cmcId);
                    if(StringUtils.isNotBlank( CMC_PRICE_CHANGFLAG )){
                        double priceCnyLast=Double.valueOf( CMC_PRICE_CHANGFLAG );
                        if(currencyQuotationResultVo.getPriceCny()>priceCnyLast){
                            currencyQuotationResultVo.setChangFlag( 1 );
                        }else if(currencyQuotationResultVo.getPriceCny()==priceCnyLast){
                            currencyQuotationResultVo.setChangFlag( 0 );
                        }else{
                            currencyQuotationResultVo.setChangFlag( -1 );
                        }
                    }
                    redisServer.set( RedisKeysUtils.CMC_PRICE_CHANGFLAG+cmcId,currencyQuotationResultVo.getPriceCny() );
                }
            }
            ApiCurrencyDiagnosisScoreVo scoreVo=new ApiCurrencyDiagnosisScoreVo();
            String redisDate = redisServer.get(RedisKeysUtils.DIAGNOSIS_CURRENCY_SCORE_INFO+cmcId);
            if (StringUtils.isNotBlank( redisDate )){
                scoreVo = JSON.parseObject(redisDate,ApiCurrencyDiagnosisScoreVo.class);
            }else {
                scoreVo = MakeCurrencyDiagnosisScore(cmcId);
            }
            if (scoreVo!=null){
                currencyQuotationResultVo.setScore(Double.parseDouble(scoreVo.getScore()));
            }
            //换手率
            if(currencyQuotationResultVo.getMarketCapCny() != null &&
                    currencyQuotationResultVo.getMarketCapCny()!=0
                    &&
                    currencyQuotationResultVo.getVolume24h()!=null
                    &&
                    currencyQuotationResultVo.getMarketCapCny()!=null){
                currencyQuotationResultVo.setTurnover(getDoubleTwo((currencyQuotationResultVo.getVolume24h()/currencyQuotationResultVo.getMarketCapCny()*100)));
            }

            ExchangeTransactionpairMiddle exchangeTransactionpairMiddle=new ExchangeTransactionpairMiddle();
            exchangeTransactionpairMiddle.setTransactionName(cmcId);
            List<ExchangeTransactionpairMiddle> exchangeTransactionpairMiddleList = exchangeTransactionpairMiddleMapper.getTransactionPairByCurrencyInfo( exchangeTransactionpairMiddle );
            if(exchangeTransactionpairMiddleList.size()>0){
                exchangeTransactionpairMiddle = exchangeTransactionpairMiddleList.get( 0 );
                //7日k线图
                List<KLine> KLine7D=new ArrayList<>(  );
                for (int i=0;i<7;i++){
                    KLine kLine=new KLine();
                    kLine.setExchangeId( exchangeTransactionpairMiddle.getExchangeId() );
                    kLine.setTransationPairName(  exchangeTransactionpairMiddle.getTransactionName());
                    kLine.setKDate( DateUtils.getNextDayDateYMD2(-i) );
                    List<KLine> kList = kLineMapper.selectKline8h( kLine );
                    KLine7D.addAll( kList );
                }
                currencyQuotationResultVo.setKLine7D( KLine7D );

                //最新价格
                String Rediskey = RedisKeysUtils.API_SERVICE_K_ACTION_ALL_DATA_PRICE + "@" + exchangeTransactionpairMiddle.getExchangeId()+ "@" + exchangeTransactionpairMiddle.getTransactionName();
                String API_SERVICE_K_ACTION_CHANGE_PRICE = redisServer.get( Rediskey );
                if(API_SERVICE_K_ACTION_CHANGE_PRICE!=null){
                    KActionTotalPo rActionTotalPo = JSON.parseObject( API_SERVICE_K_ACTION_CHANGE_PRICE,KActionTotalPo.class );
                    currencyQuotationResultVo.setPriceCny( rActionTotalPo.getOpenPriceCny()!=null? getConverCut(rActionTotalPo.getOpenPriceCny()).doubleValue():0);
                    currencyQuotationResultVo.setChangFlag(rActionTotalPo.getChangFlag()!=null?rActionTotalPo.getChangFlag():0);
                    //今日涨幅
                    currencyQuotationResultVo.setTodayGains( rActionTotalPo.getChangePrice()!=null?Double.parseDouble( rActionTotalPo.getChangePrice() ):0.00);

                    //差值计算
                    double difference=currencyQuotationResultVo.getPriceCny()*currencyQuotationResultVo.getTodayGains();
                    currencyQuotationResultVo.setDifference(getDoubleTwo( difference/100 ));
                }
                //交易对名称
                currencyQuotationResultVo.setTransactionName( exchangeTransactionpairMiddle.getTransactionName() );
            }

            if(currencyQuotationResultVo.getMarketCapCny()==null) {
                currencyQuotationResultVo.setMarketCapCny(0.00);
            }
            if(currencyQuotationResultVo.getChange24h()==null) {
                currencyQuotationResultVo.setChange24h(0.00);
            }
            if(currencyQuotationResultVo.getChange1h()==null) {
                currencyQuotationResultVo.setChange1h(0.00);
            }
            if(currencyQuotationResultVo.getCirculatingSupply()==null) {
                currencyQuotationResultVo.setCirculatingSupply( 0.00 );
            }
            if(currencyQuotationResultVo.getVolume24h()==null) {
                currencyQuotationResultVo.setVolume24h( 0.00 );
            }
            if(currencyQuotationResultVo.getScore()==null) {
                currencyQuotationResultVo.setScore( 0.00 );
            }
            if(currencyQuotationResultVo.getPriceCny()==null) {
                currencyQuotationResultVo.setPriceCny( 0.00 );
            }
            if(currencyQuotationResultVo.getLastOpenPrice()==null){
                currencyQuotationResultVo.setLastOpenPrice( 0+"" );
            }
            String highPriceKey =  RedisKeysUtils.TIR_CMC_HIGH_PRICE+cmcId;
            String lowPriceKey =  RedisKeysUtils.TIR_CMC_LOW_PRICE+cmcId;
            String highPrice = redisServer.get(highPriceKey);
            String lowPrice = redisServer.get(lowPriceKey);

            currencyQuotationResultVo.setHighPrice( highPrice );
            currencyQuotationResultVo.setLowPrice( lowPrice );

            redisServer.set( redisCacheKeyCmc,JSON.toJSONString( currencyQuotationResultVo ) , 300L);
            return currencyQuotationResultVo;
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("币种详情",e);
        }
        return null;
    }



    //保留小数点后两位位
    public static Double getDoubleTwo(Double dou){
        String b=String.format("%.2f", dou);
        Double c=Double.parseDouble(b);
        return c;
    }

    public static BigDecimal getConverCut(String lastPriceB) {
        BigDecimal result = new BigDecimal(0);
        result = org.apache.commons.lang3.StringUtils.isNotBlank(lastPriceB)?new BigDecimal(lastPriceB).setScale(10,BigDecimal.ROUND_DOWN):result;
        if(result.compareTo(BigDecimal.valueOf(1)) >=0){
            result = result.setScale(2,BigDecimal.ROUND_DOWN);
        }else{
            BigDecimal divisor = BigDecimal.ONE;
            MathContext mc = new MathContext(5);
            result = result.divide(divisor, mc);
        }
        return result;
    }

    /**
     * 根据cmsId获取 货币信息
     * @param cmsId
     * @return
     */
    public CurrencyInfo getCurrencyInfo(String cmsId){
        String currencyInfo = redisServer.hashGet(RedisKeysUtils.CURRENCY_INFO_KEY,cmsId);
        if(org.apache.commons.lang3.StringUtils.isNotBlank(currencyInfo)){
            return JSONObject.parseObject(currencyInfo,CurrencyInfo.class);
        }
        currencyInit();
        return currencyInfoMapper.selectByEntity(new CurrencyInfo().setCmcId(cmsId));

    }

    /**
     * 货币缓存 初始化
     */
    public void currencyInit(){
        List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList(new CurrencyInfo().setDeleteFlag(false).setEnableFlag(true));
        currencyInfoList.forEach(currencyInfo -> {
            currencyInfo.setImageUrl(ossImageUtil.fmtImg(currencyInfo.getImageUrl()));
            if(currencyInfo.getWeights()){
                redisServer.hashSet(RedisKeysUtils.CURRENCY_WEIGHTS_KEY,currencyInfo.getCmcId(),JSONObject.toJSONString(currencyInfo));
            }
            redisServer.hashSet(RedisKeysUtils.CURRENCY_INFO_KEY,currencyInfo.getCmcId(), JSONObject.toJSONString(currencyInfo));
        });
    }

    //保留小数点后几位小数
    public static Double RetainCount (Double f,int count){
        BigDecimal bigDecimal = new BigDecimal(f);
        //这里的 2 就是你要保留几位小数。
        double f1 = bigDecimal.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /**
     * 组装货币诊断得分到redis中
     * @param cmc_id
     * @throws Exception
     */
    public ApiCurrencyDiagnosisScoreVo MakeCurrencyDiagnosisScore(String cmc_id) throws Exception{
        ApiCurrencyDiagnosisScoreVo vo=new ApiCurrencyDiagnosisScoreVo();
        String grailStr = null;
        //获取当前小时
        String rowKey = "";
        int day =0;
        while (StringUtils.isBlank(grailStr)){//循环获取，指导获取到数据
            rowKey = DateUtils.getNextDayDateYMD(day);
            grailStr = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.CURRENCY_DIAGNOSIS_SCORE_TABLE_NAME
                    , HbaseTableUtils.CURRENCY_DIAGNOSIS_SCORE_TABLE_FAMILY
                    , HbaseTableUtils.CURRENCY_DIAGNOSIS_SCORE_QUALIFIER
                    , cmc_id+"@"+rowKey);//DateUtils.getCurrentDateString()
            day --;
            if(StringUtils.isNotBlank(grailStr)||day<-1){
                break;
            }
        }
        LogTrace.info("hbase数据查询",rowKey+"是否存在数据"+cmc_id, StringUtils.isBlank(grailStr)?"无数据":"有数据");
        if(org.apache.commons.lang3.StringUtils.isBlank(grailStr)){
            return null;
        }
        //数据库查询模型symbol
        CurrencyInfo queryInfo = new CurrencyInfo();
        queryInfo.setCmcId(cmc_id);
        Long dstart = System.currentTimeMillis();
        // CurrencyInfo infoList  =  currencyInfoMapper.selectByEntity(queryInfo);
        CurrencyInfo info = getCurrencyInfo(cmc_id);
        LogTrace.info("量化评估-获取诊断得分-redis查询","耗时",String.valueOf(System.currentTimeMillis()-dstart));
        //获取指定货币量能变化  DiagnosisQuantumEnergy
        Map<String,Object> diagnosisMap = JSON.parseObject(grailStr);
        String dataDiagnosi = "";
        int maxHour = 0;
        //获取最后数据
        for(Map.Entry<String,Object> maxMap : diagnosisMap.entrySet()){
            int tempHour = Integer.valueOf(maxMap.getKey());
            if(tempHour >= maxHour){
                maxHour = tempHour;
                //dataDiagnosi = JSON.toJSONString(diagnosisMap.get(String.valueOf(hourStr)));
                dataDiagnosi = JSON.toJSONString(maxMap.getValue());
            }
        }
        LogTrace.info("量化评估-获取诊断得分-时间",String.valueOf(maxHour), StringUtils.isBlank(dataDiagnosi)?"无数据":"有数据");
        List<DiagnosisScore> diagnosisScoreList = JSON.parseArray(dataDiagnosi,DiagnosisScore.class);
        if(diagnosisScoreList == null || diagnosisScoreList.size() == 0) return null;
        DiagnosisScore diagnosisScore = diagnosisScoreList.get(diagnosisScoreList.size()-1);
        vo.setDiagnosisId(String.valueOf(1));
        vo.setScore(diagnosisScore.getDiagnosisScore().setScale(1, BigDecimal.ROUND_DOWN).toString());
        //记录内容是否为空
        LogTrace.info("量化评估-获取诊断得分-量能、流动、市场、ar人气数据","数据",JSON.toJSONString(diagnosisScore));
        //评语
        vo.setEnergyChange(diagnosisScore.getQuantumEnergyComment()!=null?diagnosisScore.getQuantumEnergyComment():"");
        vo.setCurrencyLiquidity(diagnosisScore.getLiquidityComment()!=null?diagnosisScore.getLiquidityComment():"");
        vo.setPopularityIndex(diagnosisScore.getPopularityIndicatorComment()!=null?diagnosisScore.getPopularityIndicatorComment():"");
        vo.setMarketHeat(diagnosisScore.getTrendsInfoResult()!=null && diagnosisScore.getTrendsInfoResult()!=null?diagnosisScore.getTrendsInfoResult():""); //市场热度评语
        vo.setWinPercent(diagnosisScore.getDefect()!=null?diagnosisScore.getDefect().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"0");  //打败
        //雷达图数据插入顺序 市场热度，人气指标，货币流动性，量能变化
        List<String> indicator = new ArrayList<>();
        indicator.add(diagnosisScore.getTrendsInfoScore()!=null&&diagnosisScore.getTrendsInfoScore()!=null?diagnosisScore.getTrendsInfoScore().toString():"0");//市场待取
        indicator.add(diagnosisScore.getPopularityIndicatorScore()!=null?diagnosisScore.getPopularityIndicatorScore().toString():"0");//ar人气指标
        indicator.add(diagnosisScore.getLiquidityScore()!=null?diagnosisScore.getLiquidityScore().toString():"0"); //货币流动性
        indicator.add(diagnosisScore.getQuantumEnergyScore()!=null?diagnosisScore.getQuantumEnergyScore().toString():"0");//量能变化
        vo.setIndicator(indicator);
        if(info!=null){
            vo.setSymbol(info.getSymbol());//插入模型
            vo.setCoinNameCn(info.getCoinNameCn());
            if(info.getImageUrl()!=null){
                if(info.getImageUrl().contains( "http" )){
                    vo.setImageUrl( info.getImageUrl() );
                }else{
                    String imgUrl = ossTool.getOSSUrl( info.getImageUrl() );
                    vo.setImageUrl( imgUrl );
                }
            }
        }

        redisServer.set(RedisKeysUtils.DIAGNOSIS_CURRENCY_SCORE_INFO+cmc_id,JSON.toJSONString(vo),300L);
        return  vo;
    }

}
